#!/usr/bin/php
<?php

ini_set('display_errors','On');
error_reporting(1);

Class Advert_Import
{
var $production;
var $development;

function Advert_Import()
{
## PREPARE CONNECTION ##

$this->production=mysql_connect("localhost","infopark_com_ua","8888zG6Wtv1k",true);
mysql_set_charset ('utf8',$this->production);
mysql_select_db("dev_infopark_com_ua",$this->production);

$this->development=mysql_connect("localhost","infopark_com_ua","8888zG6Wtv1k",true);
mysql_set_charset ('utf8',$this->development);
mysql_select_db("dev_infopark_com_ua1",$this->development);

#$this->mig_taxonomy_term_data();
#$this->mig_taxonomy_term_hierarchy();
#$this->mig_node_advert_terms_taxonomy();
print "Need write update logic\n\n";
mysql_close($this->development);
mysql_close($this->production);
}
function mig_taxonomy_term_data()
{
  $sql="SELECT * FROM taxonomy_term_data WHERE vid=12";
  $taxonomy_term_data=$this->LIST_Q($sql,$this->production);
  foreach($taxonomy_term_data as $term)
  {
  $sql1="INSERT INTO taxonomy_term_data (vid,name,description,format,weight,old_cat_id,dev_id) VALUES (12,".$this->Q($term->name,1).",".$this->Q($term->description,1).",".$this->Q($term->format,1).",".$this->Q($term->weight).",".$this->Q($term->old_cat_id,1).",".$this->Q($term->tid,1).")";
  print $sql1."\n";
  $this->QUERY($sql1,$this->development);
  $tid=mysql_insert_id($this->development);
  }
}
function mig_taxonomy_term_hierarchy()
{
  $sql="SELECT * FROM taxonomy_term_data WHERE vid=12";
	$taxonomy_term_data=$this->LIST_Q($sql,$this->development);
  foreach($taxonomy_term_data as $term)
  {
  $sql1="SELECT * FROM taxonomy_term_hierarchy WHERE tid=".$term->dev_id;
  $taxonomy_term_hierarchy=$this->ROW_Q($sql1,$this->production);
  if($taxonomy_term_hierarchy->parent)
  {
  $sql2="SELECT * FROM taxonomy_term_data WHERE dev_id=".$taxonomy_term_hierarchy->parent;
  $taxonomy_term_data1=$this->ROW_Q($sql2,$this->development);
  $sql3="INSERT INTO taxonomy_term_hierarchy (tid,parent) VALUES (".$this->Q($term->tid).",".$this->Q($taxonomy_term_data1->tid).")";
  }else{
  $sql3="INSERT INTO taxonomy_term_hierarchy (tid,parent) VALUES (".$this->Q($term->tid).",0)";
  }
  print $sql3."\n";
  $this->QUERY($sql3,$this->development);
  
  
  }
}
function mig_node_advert_terms_taxonomy()
{
 $sql="SELECT * FROM taxonomy_term_data WHERE vid=12";
	$taxonomy_term_data=$this->LIST_Q($sql,$this->development);
  foreach($taxonomy_term_data as $term)
  {
  $sql1="SELECT * FROM node_advert_terms_taxonomy WHERE tid=".$term->dev_id;
  print $sql1."\n";
  $node_advert_terms_taxonomy=$this->ROW_Q($sql1,$this->production);
  #pre($node_advert_terms_taxonomy);
  if($node_advert_terms_taxonomy->node_advert_terms_id)
  {
  $sql2="INSERT INTO node_advert_terms_taxonomy (tid,node_advert_terms_id) VALUES (".$this->Q($term->tid).",".$this->Q($node_advert_terms_taxonomy->node_advert_terms_id).")";
  
  print $sql2."\n";
  $this->QUERY($sql2,$this->development);
  }
  #die('###stop');
  }
}





function Q($str,$string=0)
{
return ($string?"'":"").mysql_escape_string($str).($string?"'":"");
}
function QUERY($sql,$conn)
{
#$$result=mysql_query($sql,$conn);
$result=mysql_query($sql,$conn) or die($sql."\n\n".mysql_error($conn));
return $result;
}
function ROW_Q($sql,$conn)
{
$result=$this->QUERY($sql,$conn);
$r=mysql_fetch_object($result);
if(count($r)==0)$r=false;
return $r;
}
function LIST_Q($sql,$conn)
{

$result=$this->QUERY($sql,$conn);
while($r=mysql_fetch_object($result))
  {
  $data[]=$r;
  }
  
  if(count($data)==0)$data=false;
  return $data;
}

}
function pre($array)
{
print_r($array);
}

$AI=new Advert_Import;


print "### GOOD FINALL ###\n\n";
?>